package util;

import sun.misc.ProxyGenerator;

import java.io.File;
import java.io.FileOutputStream;
import java.nio.file.Paths;

/**
 * @author wangpp
 */
public class ProxyUtil {
    public static void saveJDKRuntimeClassToFile(Object obj, String filename) {
        byte[] bytes = ProxyGenerator.generateProxyClass(obj.getClass().getSimpleName(), obj.getClass().getInterfaces());
        try {
            filename = filename.endsWith(".class") ? filename : filename + ".class";
            File file = Paths.get(filename).toFile();
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            fileOutputStream.write(bytes, 0, bytes.length);
            fileOutputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
